package com.clkj.xiuzheng.service.shangcheng;

import com.clkj.xiuzheng.dao.shangcheng.*;
import com.clkj.xiuzheng.pojo.common.DateFormatHelper;
import com.clkj.xiuzheng.pojo.shangcheng.Identity;
import com.clkj.xiuzheng.pojo.shangcheng.IdentityInfo;
import com.clkj.xiuzheng.pojo.shangcheng.Menu;
import com.clkj.xiuzheng.pojo.shangcheng.Operator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

@Service
public class ScOperatorServiceImpl implements ScOperatorService{

    @Autowired
    private ScOperatorMapper scOperatorMapper;
    @Autowired
    private ScIdentityMapper scIdentityMapper;
    @Autowired
    private SCMenuMapper scMenuMapper;
    @Autowired
    private ScCompanyMapper scCompanyMapper;
    @Autowired
    private IdentityInfoMapper identityInfoMapper;

    @Override
    public PageInfo getOperatorList(Integer companyId, String operatorName, String createTime, String endTime, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<Operator> operatorList = scOperatorMapper.getOperatorList(companyId,operatorName,createTime,endTime);
        return new PageInfo<>(operatorList);
    }

    @Override
    public void addOperator(Integer companyId, String operatorName) {
        Operator operator = new Operator();
        operator.setOperatorName(operatorName);
        Random random = new Random();
        int randomNumber1 = random.nextInt(900000) + 100000;
        operator.setOperatorAccount(String.valueOf(randomNumber1));
        int randomNumber2 = random.nextInt(900000) + 100000;
        operator.setOperatorPassword(String.valueOf(randomNumber2));
        operator.setCreateTime(DateFormatHelper.today());
        operator.setOperatorStatus(1);
        operator.setCompanyId(companyId);
        scOperatorMapper.insertSelective(operator);
    }

    @Override
    public void updateOperatorStaus(String Ids, Integer status) {
        String[] split = Ids.split(",");
        Example example= new Example(Operator.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("operatorId", Arrays.asList(split));
        Operator operator = new Operator();
        operator.setOperatorStatus(status);
        scOperatorMapper.updateByExampleSelective(operator,example);
    }

    @Override
    public List<Identity> getIdentityList(Integer companyId) {
        Example example= new Example(Identity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("companyId",companyId);
        List<Identity> identities = scIdentityMapper.selectByExample(example);
        return identities;
    }

    @Override
    public void updateIdentity(Integer operatorId, Integer identityId) {
        Operator operator= new Operator();
        operator.setIdentityId(operatorId);
        operator.setOperatorId(identityId);
        scOperatorMapper.updateByPrimaryKeySelective(operator);
    }

    @Override
    public List<Menu> getMenuListByCompanyId() {
        Example example= new Example(Menu.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status",1);
        criteria.andEqualTo("parentId",0);
        List<Menu> menus = scMenuMapper.selectByExample(example);
        menus.forEach(menu -> {
            Example example1= new Example(Menu.class);
            Example .Criteria criteria1 = example1.createCriteria();
            criteria1.andEqualTo("status",1);
            criteria1.andEqualTo("parentId",menu.getMenuId());
            List<Menu> menus1 = scMenuMapper.selectByExample(example1);
            menu.setChildren(menus1);
        });
        return menus;
    }

    @Override
    @Transactional
    public void addIdentity(String ids, String identityName, Integer companyId) {
        String[] split = ids.split(",");
        Identity identity = new Identity();
        identity.setIdentityName(identityName);
        identity.setCompanyId(companyId);
        scIdentityMapper.addIdentity(identity);
        List<IdentityInfo> identitieinfo = new ArrayList<>();
        for (String id : split){
            IdentityInfo identityInfo = new IdentityInfo();
            identityInfo.setIdentityId(identity.getIdentityId());
            identityInfo.setMenuId(Integer.parseInt(id));
            identitieinfo.add(identityInfo);
        }
        identityInfoMapper.insertList(identitieinfo);

    }

    @Override
    public void updatedentity(Integer IdentityId,String ids, String identityName, Integer companyId) {
        //根据身份id删除所有数据
        Example example= new Example(IdentityInfo.class);
        example.createCriteria().andEqualTo("identityId",IdentityId);
        identityInfoMapper.deleteByExample(example);
        //如果名字不为空修改名字
        if (identityName!=null || identityName!=""){
            Identity identity = new Identity();
            identity.setIdentityId(IdentityId);
            identity.setIdentityName(identityName);
            scIdentityMapper.updateByPrimaryKeySelective(identity);
        }
        //新增权限
        String[] split = ids.split(",");
        List<IdentityInfo> identitieinfo = new ArrayList<>();
        for (String id : split){
            IdentityInfo identityInfo = new IdentityInfo();
            identityInfo.setIdentityId(IdentityId);
            identityInfo.setMenuId(Integer.parseInt(id));
            identitieinfo.add(identityInfo);
        }
        identityInfoMapper.insertList(identitieinfo);

    }


}
